import axios from 'axios';
import constants from '../constants/index';
import configs from './common/configs.js';
import {message} from 'antd';
import {getTokenHeader} from '../../utils/utils';
import Qs from 'qs';
const {GET_REVIEW_INFO_SUCCESS,GET_REVIEW_INFO_FAILURE,GET_RESULT_SCR_SUCCESS,GET_PDF_SUCCESS} = constants;
const baseUrl = configs.baseUrl;

export function getReviewInfo(query = '') {
    return async (dispatch) => {
        try{
            let headers=getTokenHeader({});
            const data = (await axios.get(`${baseUrl}queryResume.do?${Qs.stringify(query)}`,{
                headers:headers
            })).data;
            if(data.code==="SUCCESS")
             {
                dispatch({
                    type: GET_REVIEW_INFO_SUCCESS,
                    data: data.data.pagingList,
                    total:data.data.total
                })
            }
            else{
                message.error('获取审核信息失败:'+data.msg);
            }
        }catch(e){
            message.error('获取审核信息失败');
            dispatch({
                type: GET_REVIEW_INFO_FAILURE,
                error: new Error(e)
            })
        }
    }
}


/**
 * 提交审核
 * @param {*} query 
 */
export function submitAudit(query=''){
    return async dispatch=>{
        try{
            let headers=getTokenHeader({});
            axios({
                method: 'PUT',
                url: `${baseUrl}resume/examine.do`,
                data: JSON.stringify(query),
                headers: {
                    'Content-Type': 'application/json',
                    ...headers
                    },
                dataType:'json',
            }).then((data)=>{
                if(data.data.code==="SUCCESS"){
                    message.success("审核成功");
                }else{
                    message.warning("审核失败失败:"+data.msg);
                }
            })
    
        }catch(error){
            message.warning("审核失败");
        }
    }
}

/**
 * 获取成绩文件信息
 * @param {*} query 
 */
export function getResultFileInfor(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}file/scoreFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_RESULT_SCR_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error("成绩文件获取失败"+data.msg);
            }
        } catch (error) {
            message.error('成绩文件获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 删除附件信息
 * @param {*} query 
 */
export function deleteResultFile(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.delete(`${baseUrl}file/scoreFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
               message.success('删除成功');
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('删除失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}


/**
 * 导出表格信息
 * @param {*} query 
 */
export function exportTable(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}exportResume.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
               message.success('导出成功');
            }
            else{
                message.error('导出失败',data.msg);
            }
        } catch (error) {
            message.error('导出失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 获取报名文件
 * @param {*} query 
 */
export function getPdf(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}resumeHtml.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_PDF_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error("报名信息文件获取失败"+data.msg);
            }
        } catch (error) {
            message.error('报名信息文件获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 获取附件文件
 * @param {*} query 
 */
export function getFujian(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}file/prizeFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_PDF_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error("附件获取失败"+data.msg);
            }
        } catch (error) {
            message.error('附件获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}